<?php
require __DIR__ . '/vendor/autoload.php';
use think\App;
use Workerman\Worker;
use app\util\rke\Aio;
use think\Facade\Env;
use app\model\AdminSchoolDevice;

/**
 * 人脸机对接
 */
$app = new App();
$http = $app->http;
$http->run();
$worker = new Worker();

$worker->onWorkerStart = function (){
    $host = Env::get('FACE_DEVICE.FD_HOST','127.0.0.1');
    $port = Env::get('FACE_DEVICE.FD_PORT',61613);
    $keepalive = 30;
    $cleanSession = true;
    $clientId = rand(0, 100000) . time();
    $options = [
        'username' => Env::get('FACE_DEVICE.FD_USERNAME',''),
        'password' => Env::get('FACE_DEVICE.FD_PASSWORD',''),
        'keepalive' => $cleanSession,
        'clean_session' => $keepalive,
        'client_id' => $clientId,
    ];
    $click = "mqtt://$host:$port";
    $mqtt = new Workerman\Mqtt\Client($click, $options);
    $mqtt->onConnect = function ($mqtt){
        $mqtt->subscribe('mqtt/face/basic');
        $mqtt->subscribe('mqtt/face/heartbeat');
        $rls = AdminSchoolDevice::getFaceBySchoolList();
        foreach ($rls as $va){
            $mqtt->subscribe('mqtt/face/'.$va['serial'].'/QRCode');
            $mqtt->subscribe('mqtt/face/'.$va['serial'].'/Ack');
            $mqtt->subscribe('mqtt/face/'.$va['serial'].'/Rec');
            $mqtt->subscribe('mqtt/face/'.$va['serial'].'/Snap');
            $mqtt->subscribe('mqtt/face/'.$va['serial'].'/IDCard');
            $mqtt->subscribe('mqtt/face/'.$va['serial'].'/Card');
            $mqtt->subscribe('mqtt/face/'.$va['serial'].'/Alarm');
        }
    };
    $aio = new Aio($mqtt);
    $mqtt->onMessage = function($topic, $content) use ($mqtt,$aio){
        $result = json_decode($content,true);
        if(!empty($result['info']) && !empty($result['info']['facesluiceId'])){
            $facesluice = explode('-',$result['info']['facesluiceId']);
            $result['info']['facesluiceId'] = $facesluice[0];
            $result['info']['school_id'] = $facesluice[1];
        }
//        \think\facade\Log::error('接收人脸机包');
//        \think\facade\Log::error($result);
        switch ($result['operator']){
            case 'HeartBeat': // 心跳
                $aio->handleDevice($result['info']);
                break;
            case 'Online':
                $mqtt->subscribe('mqtt/face/'.$result['info']['facesluiceId'].'/QRCode');
                $mqtt->subscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Ack');
                $mqtt->subscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Rec');
                $mqtt->subscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Snap');
                $mqtt->subscribe('mqtt/face/'.$result['info']['facesluiceId'].'/IDCard');
                $mqtt->subscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Card');
                $mqtt->subscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Alarm');
                $aio->handleOnline($result['info']);
                break;
            case 'Offline':
                $mqtt->unsubscribe('mqtt/face/'.$result['info']['facesluiceId'].'/QRCode');
                $mqtt->unsubscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Ack');
                $mqtt->unsubscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Rec');
                $mqtt->unsubscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Snap');
                $mqtt->unsubscribe('mqtt/face/'.$result['info']['facesluiceId'].'/IDCard');
                $mqtt->unsubscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Card');
                $mqtt->unsubscribe('mqtt/face/'.$result['info']['facesluiceId'].'/Alarm');
                break;
            case 'QRCodePush': // 二维码扫码信息推送
                $aio->qrPush($result['info']);
                break;
            case 'Unlock-Ack':
                $aio->handleAccessRecord($result);
                break;
            case 'RecPush': // 人脸识别上传
                $aio->handleAccessFaceRecord($result);
                break;
            case 'CardPush': // 刷卡上传
                \think\facade\Log::error($result);
                break;
            case 'EditPerson': // 人员信息推送
                //\think\facade\Log::error($result);
                break;
            default:
                //echo "topic:$topic content:$content\n";
        }
        //echo "topic:$topic content:$content\n";
    };
    $mqtt->connect();
};
Worker::$pidFile=__DIR__.'/runtime/wm.pid';
Worker::runAll();